/*
 * @Author: wangshiyang
 * @Date: 2023-04-27 11:28:20
 * @LastEditors: your name
 * @LastEditTime: 2024-01-25 17:19:15
 * @Description: 请填写简介
 */
/**
 * 自定义材质线 github:cesium-materialLine
 * options {MaterialType,image,color,duration}
 * @param {*} options
 * @returns
 */
 import * as Cesium from "cesium";

 function CustomMaterialLine(options) {
   let Color = Cesium.Color,
     defaultValue = Cesium.defaultValue,
     defined = Cesium.defined,
     defineProperties = Object.defineProperties,
     Event = Cesium.Event,
     createPropertyDescriptor = Cesium.createPropertyDescriptor,
     Property = Cesium.Property,
     Material = Cesium.Material,
     defaultColor = Color.WHITE,
     currentTime = window.performance.now(),
     MaterialType = "flowLine";
   function PolylineCustomMaterialProperty(options) {
     options = defaultValue(options, defaultValue.EMPTY_OBJECT);
     this._definitionChanged = new Event();
     this._color = undefined;
     this._colorSubscription = undefined;
     this.color = options.color || Cesium.Color.BLUE;
     this.duration = options.duration || 1000;
     this.repeat = options.repeat || 1;
     this._time = currentTime;
   }
   defineProperties(PolylineCustomMaterialProperty.prototype, {
     isConstant: {
       get: function () {
         return false;
       },
     },
     definitionChanged: {
       get: function () {
         return this._definitionChanged;
       },
     },
     color: createPropertyDescriptor("color"),
   });
   PolylineCustomMaterialProperty.prototype.getType = function (time) {
     return MaterialType;
   };
   PolylineCustomMaterialProperty.prototype.getValue = function (time, result) {
     if (!defined(result)) {
       result = {};
     }
     result.color = Property.getValueOrClonedDefault(
       this._color,
       time,
       defaultColor,
       result.color
     );
     result.image = options.image;
     result.time =
       ((window.performance.now() - this._time) % this.duration) / this.duration;
     result.repeat = options.repeat;
     return result;
   };
   PolylineCustomMaterialProperty.prototype.equals = function (other) {
     return (
       this === other ||
       (other instanceof PolylineCustomMaterialProperty &&
         Property.equals(this._color, other._color))
     );
   };
   PolylineCustomMaterialProperty.prototype.isTranslucent = function () {
     return true;
   };
 
   Material._materialCache.addMaterial(MaterialType, {
     fabric: {
       type: MaterialType,
       uniforms: {
         color: options.color || new Cesium.Color(1.0, 0.0, 0.0, 0.5),
         image: options.image,
         repeat: options.repeat,
         duration:options.duration
       },
       source: /*glsl*/`czm_material czm_getMaterial(czm_materialInput materialInput)
                 {
                     float time=czm_frameNumber/(120.0*duration/1000.0);
                    //  获取需要输出的纹理坐标
                     czm_material material = czm_getDefaultMaterial(materialInput);
                    //  获取当前实体上的uv坐标
                     vec2 st = materialInput.st;
                    //  取图片纹理的透明度分量
                     material.alpha=texture2D(image, vec2(fract(st.s*repeat-time),st.t)).a;
                    //  取外部颜色的rgb分量
                     material.diffuse = color.rgb;
                     return material;
                 }
                 `,
     },
     translucent: function (material) {
       return true;
     },
   });
   return new PolylineCustomMaterialProperty(options);
 }
 
 export default CustomMaterialLine;
 